package com.linkage.educloud.ah.view.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.mvp.contract.mine.IFeedbackContract;
import com.linkage.educloud.ah.mvp.presenter.mine.FeedbackPresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.zzhou.common.library.util.ToastShow;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/11.
 */

public class FeedbackActivity extends BasicTitleActivity implements IFeedbackContract.IView {

    @BindView(R.id.ac_feedback_ed)
    EditText ac_feedback_ed;
    @BindView(R.id.ac_feedback_num)
    TextView ac_feedback_num;
    @BindView(R.id.ac_feedback_submit)
    Button ac_feedback_submit;

    IFeedbackContract.IPresenter iPresenter;

    public static void launch(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new FeedbackPresenter(this, this);
        setTitle("反馈意见");
        addTextChangedListener();

        ac_feedback_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ac_feedback_ed.getText().toString().trim().isEmpty()) {
                    ToastShow.show(FeedbackActivity.this, "反馈意见不能为空!");
                    return;
                }
                showZloading();
                iPresenter.sendMachineRecord(ac_feedback_ed.getText().toString().trim());
            }
        });
    }

    private void addTextChangedListener() {
        ac_feedback_ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = s.length();
                ac_feedback_num.setText(number + "/240");
                if (number == 240) {
                    ac_feedback_num.setTextColor(getResources().getColor(R.color.tab_color_true));
                } else {
                    ac_feedback_num.setTextColor(getResources().getColor(R.color.dark_gray));
                }
            }
        });
    }

    public void hidezloading(){
        hideZloading();
    }
}
